package com.xy.wsl.api.controller.DouYin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xy.wsl.api.model.MsgCheckRequest;
import com.xy.wsl.api.response.ResponseResult;
import com.xy.wsl.api.sql.entity.ThirdApp.ThirdApp;
import com.xy.wsl.api.sql.services.ThirdApp.ThirdAppService;
import com.xy.wsl.api.utils.HttpUtil;
import com.xy.wsl.api.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/thirdApp")
@Slf4j
@RestController
@ResponseResult
public class DouYinController {

    @Autowired
    ThirdAppService appService;

    @PostMapping("msgcheck")
    public Object msgCheck(@RequestBody MsgCheckRequest request, String appid) {

        ThirdApp app = com.xy.wsl.api.utils.ThirdApp.getAppAndToken(appid,appService);
        List<JSONObject> list  = new ArrayList<>();
        list.add(JsonUtil.create("content",request.getContent()));
        JSONObject jsonObject = HttpUtil.DouyinMsgcheck(JsonUtil.obj2json(JsonUtil.create("tasks",list)),
                "https://developer.toutiao.com/api/v2/tags/text/antidirt",app.getToken());
        JSONArray jsonArray = jsonObject.getJSONArray("data");

        boolean hid = jsonArray.stream().filter(a -> ((JSONObject)a)
                .getJSONArray("predicts").stream().filter(c ->((JSONObject)c).getBoolean("hit"))
                .count() > 0).count() > 0;
        if (hid) {
            throw new RuntimeException("当前信息存在敏感信息，请修改后重新上传");
        }
        return "正常";
    }
}
